home *** CD-ROM | disk | FTP | other *** search
- Path: news.u-net.com!news
- From: charlotte@parmo.u-net.com (Thomas Christensen)
- Newsgroups: comp.lang.c++
- Subject: Re: Call order of constructors
- Date: Fri, 23 Feb 1996 09:55:43 GMT
- Organization: U-NET limited
- Message-ID: <4gk30t$pqt@nuntius.u-net.net>
- References: <4ftv2n$kts@ccnet3.ccnet.com>
- Reply-To: thc@mailhost.net
- NNTP-Posting-Host: parmo.u-net.com
- X-Newsreader: Forte Free Agent v0.55
-
- jantypas@ccnet.com (John Antypas) wrote:
-
- >Hello C++ wizards...
-
- >I'm trying to create a library access some private netowrk resources.
- >I thought I'd try something like this:
-
- >class TRANSPORT {
- > ... magic stuff...
- >public:
- > TRANSPORT(char *name) // Called to activate and bind transport(name)
- > ~TRANSPORT() // Called when we're done with transport(name)
-
- >}
-
- >class SESSION(
- > TRANSPORT transport&;
- > ....
- > SESSION(char *transport_name,
- > char *transport_address);
- > ~SESSION();
- > ....
- >}
-
- >I was hoping that main code such as :
-
- > SESSION my_session("iee488", "unit-4B");
-
- >would create a session on the IEEE488 controller, to unit Unit-4B, specifically, - open a trasnport to the IEEE-488 system by calling the IEEE488
- > constructor
- > - open a session by calling the session constructor and have it
- > finish the job.
-
- >The destructor sequence runs in the exact reverse.
-
- >First off, the compiler complains about the TRANSPORT constructors, second,
- >I find myself asking what to do if a constructor or destructor fails?
- >How do I catch the error condition to notify outer levels of the program?
-
- >Help as always is appreciated.
-
- >John Antypas jantypas@soft21.s21.com
-
- Hi John
-
- 1. The constructor situation
- I would change the SESSION class to something like this:
-
- class SESSION(
- TRANSPORT &transport;
- ....
- SESSION(char *transport_name, char *transport_address)
- {
- transport = NULL;
- transport = new TRANSPORT(transport_address);
- }
- ~SESSION()
- {
- if (transport)
- delete transport;
- }
- ....
- }
-
- In this way you control the construction of the TRANSPORT object.
-
- 2. The error handling situation
- You said the word: catch!
- I would use exception handling to catch the errors so if the TRANSPORT
- couldn't connect then I throw an exception, which you'd have to catch
- in the main program like this:
- main()
- {
- try
- {
- SESSION my_session("iee488", "unit-4B");
- }
- catch(char *text)
- {
- cout << "Exception thrown: " << text << endl;
- // Do some more magic...
- }
- ...
- }
-
- The exception need to be thrown from anyone of the TRANSPORT or
- SESSION classes, and the syntax is like that of a return statement:
- throw "Transport object couldn't connect!";
- You can even throw a c++ class containing further description of the
- exception ect.
- The real power of C++ exception handling lies not only in its ability
- to deal with exceptions of varying types, but also in its ability to
- automatically call destructor functions during stack unwinding for all
- local objects constructed before the exception was thrown.
-
- 3. The alternative solution situation
- As always there's numerous ways to do this. Here's how Microsoft
- would do it:
-
- Initializing Member Objects
- Classes can contain member objects of class type, but to ensure that
- initialization requirements for the member objects are met, one of the
- following conditions must be met:
- ╖ The contained objectÆs class requires no constructor.
- ╖ The contained objectÆs class has an accessible default
- constructor.
- ╖ The containing classÆs constructors all explicitly initialize the
- contained object.
-
- The following example shows how to perform such an initialization:
-
- // Declare a class Point.
- class Point
- {
- public:
- Point( int x, int y ) { _x = x; _y = y; }
- private:
- int _x, _y;
- };
-
- // Declare a rectangle class that contains objects of type Point.
- class Rect
- {
- public:
- Rect( int x1, int y1, int x2, int y2 );
- private:
- Point _topleft, _bottomright;
- };
-
- // Define the constructor for class Rect. This constructor
- // explicitly initializes the objects of type Point.
- Rect::Rect( int x1, int y1, int x2, int y2 ) :
- _topleft( x1, y1 ), _bottomright( x2, y2 )
- {
- }
-
- The Rect class, shown in the preceding example, contains two member
- objects of class Point. Its constructor explicitly initializes the
- objects _topleft and _bottomright. Note that a colon follows the
- closing parenthesis of the constructor (in the definition). The colon
- is followed by the member names and arguments with which to initialize
- the objects of type Point.
-
- Warning The order in which the member initializers are specified in
- the constructor does not affect the order in which the members are
- constructed; the members are constructed in the order in which they
- are declared in the class.
-
- Reference and const member objects must be initialized using the
- member initialization syntax shown in Syntax in Initializing Bases and
- Members. There is no other way to initialize these objects.
-
- Hope you can use some of this...
-
- Thomas Christensen
- E-me-at: thc@mailhost.net
-
-
-